package com.niit.hjw.avgupricebycomm;

import com.niit.util.DataConvUtils;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;


public class AvgUPriceByCommReducer extends Reducer<Text, Text, Text, Text> {
    private Text outv = new Text();
    private int unitPriceSum;
    private int total;// 相同小区的出现次数

    /**
     * reduce阶段的核心业务逻辑（根据相同小区名 进行统计平均单价，然后输出）
     *
     * @param key     小区名称, eg: 香水鸿门...
     * @param values  单价, eg: 13189元/㎡...
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        for (Text value : values) {
            String[] datas = value.toString().split("元");
            unitPriceSum += DataConvUtils.strToInt(datas[0], 0);
            total++;
        }
        outv.set((unitPriceSum / total) + "元/㎡");
        context.write(key, outv);
        unitPriceSum = 0;
        total = 0;
    }
}
